﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;

namespace ARR.DataModel.Repositories
{
    public static partial class InventoriesRepository
    {
        public static void UpdateFromTempInventories()
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 900;
                database.NewCommand("Inventories_UpdateFromTempInventories", CommandType.StoredProcedure);
                database.ExecuteNonQuery();
            }
        }

        public static void InsertFromTempInventories(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 900;
                database.NewCommand("Inventories_InsertFromTempInventories", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);
                
                database.ExecuteNonQuery();
            }
        }

        public static object[] GetTotalStoresAndLastUpdate(int rangeReviewId)
        {
            object[] ret = new object[2];

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.NewCommand("Inventories_SelectTotalStoresAndLastUpdate", CommandType.StoredProcedure);
                database.AddInParameter("rangeReviewId", DbType.Int32, rangeReviewId);
                
                IDataReader reader = database.ExecuteReader();

                try
                {
                    reader.Read();

                    ret[0] = Util.CastDbValue<int>(reader["TotalStores"]); //Total de tiendas.
                    ret[1] = Util.CastDbValue<DateTime>(reader["LastDate"]); //Ultima fecha.

                    reader.Close();
                }
                catch
                {
                    return null;
                }

            }

            return ret;
        }

        
    }
}
